/**
 * @file StoreItemActivity.java
 * @create 下午5:42:34
 * @author CX
 * @description 
 */
package cn.edu.hstc.wm.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
import cn.edu.hstc.wm.AppApplication;
import cn.edu.hstc.wm.Constants;
import cn.edu.hstc.wm.R;
import cn.edu.hstc.wm.adapter.StoreItemAdapter;
import cn.edu.hstc.wm.bean.Store;
import cn.edu.hstc.wm.https.HttpUtils;
import cn.edu.hstc.wm.ui.base.BaseFragmentActivity;
import cn.edu.hstc.wm.ui.fragment.NetworkDisableFragment;
import cn.edu.hstc.wm.ui.fragment.StoreMode;
import cn.edu.hstc.wm.ui.fragment.StoreItemInfo;
import cn.edu.hstc.wm.ui.fragment.StoreItemMenu;
import cn.edu.hstc.wm.utils.IntentUtil;
import cn.edu.hstc.wm.utils.LogUtil;
import cn.edu.hstc.wm.widget.CustomToast;
import cn.edu.hstc.wm.widget.PagerSlideTab;

/**
 * 显示店铺的界面,包括店铺菜单,店铺信息两个Fragment
 * 
 */
public class StoreItemActivity extends BaseFragmentActivity implements OnClickListener, NetworkDisableFragment.CallBack {

	public static final String TAG = StoreItemActivity.class.getName();

	public static final String INTENT_URL = StoreMode.INTENT_URL;

	private Context mContext;
	private int count = 0;

	/** Actionbar上的返回,查询,更多按钮 */
	private ImageButton mBackHome;
	private ImageView mQuery, mMore;
	private PopupWindow mPopupWindow;
	private PagerSlideTab mPagerTab = null;
	private ViewPager mViewPager = null;

	/** tab上的标题 */
	private final String[] TITLES = { "店铺菜单 ", "店铺信息" };
	/** 放入ViewPager显示的Fragment */
	private List<Fragment> mFragmentList = null;
	private StoreItemMenu mItemMenu;

	/** 网络错误的提示界面 */
	private NetworkDisableFragment mDisableFragment = null;
	/** 该店铺类实体 */
	private Store mStore;
	/** 携带的数据-->指定 的店铺实体类 */
	private Bundle mBundle;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_store_item_layout);
		mContext = this;

		LogUtil.i(TAG, getIntent().getExtras().getSerializable(StoreMode.INTENT_URL).toString());

		// 从上个fragment获取到传递过来的数据
		mBundle = getIntent().getExtras();

		initActionBar();
		initShow();
	}

	@Override
	public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
		super.startActivityForResult(intent, requestCode, options);
		Log.d(TAG, "startActivityForResult " + " requestCode " + requestCode);
	}

	@Override
	protected void onDestroy() {
		LogUtil.i(TAG, "onDestroy");
		super.onDestroy();
	}

	/**
	 * 根据网络状况来显示不同的界面
	 */
	private void initShow() {
		Log.d(TAG, "net status " + AppApplication.netWorkStatus);
		// 网络不正常
		if (AppApplication.netWorkStatus < 2) {
			hideMoreView(true);
			hideQueryView(true);
			showNetWorkStatus(0);
		}
		// 网络正常
		else {
			hideMoreView(false);
			hideQueryView(false);
			showNetWorkStatus(2);
			initViewPager();
			initPagerTab();
		}
	}

	/**
	 * 显示网络的情况界面,如果网络没开则显示设置界面,如果网络正常则显示加载的内容
	 * 
	 * @param netType
	 */
	private void showNetWorkStatus(int netType) {
		FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

		/* 网络不正常,显示设置提示界面 */
		if (netType < 2) {
			mDisableFragment = NetworkDisableFragment.getInstance(null);
			transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
			transaction.replace(R.id.fl_store_item_show, mDisableFragment).commit();
		}
		/* 网络正常 ,移除设置界面 */
		else {
			if (mDisableFragment != null) {
				transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
				transaction.remove(mDisableFragment).commit();
			}
		}
	}

	/** 隐藏更多按钮 */
	public void hideMoreView(boolean flag) {
		mMore.setVisibility(flag ? View.INVISIBLE : View.VISIBLE);
	}

	/** 隐藏搜索按钮 */
	public void hideQueryView(boolean flag) {
		mQuery.setVisibility(flag ? View.INVISIBLE : View.VISIBLE);
	}

	/** 初始化Actionbar */
	private void initActionBar() {
		mBackHome = (ImageButton) findViewById(R.id.ib_actionbar_back_home);
		mQuery = (ImageView) findViewById(R.id.iv_actionbar_query);
		mMore = (ImageView) findViewById(R.id.iv_actionbar_more);

		mBackHome.setOnClickListener(this);
		mQuery.setOnClickListener(this);
		mMore.setOnClickListener(this);

		initPopupWindow();
	}

	private void initPopupWindow() {
		LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
		View layout = inflater.inflate(R.layout.main_popupwindow, null);

		ListView popupListView = (ListView) layout.findViewById(R.id.lv_popup_list);
		ArrayList<Map<String, String>> arrayList = new ArrayList<Map<String, String>>();
		Map<String, String> map;
		map = new HashMap<String, String>();
		map.put("type", "分享");
		arrayList.add(map);
		map = new HashMap<String, String>();
		map.put("type", "更多");
		arrayList.add(map);

		map = new HashMap<String, String>();
		map.put("type", "更更多");
		arrayList.add(map);

		SimpleAdapter adapter = new SimpleAdapter(mContext, arrayList, R.layout.main_popupwindow_item, new String[] { "type" },
				new int[] { R.id.tv_popupwindow_title });

		mPopupWindow = new PopupWindow(layout);
		// mPopupWindow = new PopupWindow(layout, LayoutParams.WRAP_CONTENT,
		// LayoutParams.WRAP_CONTENT);
		mPopupWindow.setFocusable(true);
		popupListView.setAdapter(adapter);
		// 控制popupwindow的宽度和高度自适应
		popupListView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
		mPopupWindow.setWidth(popupListView.getMeasuredWidth());
		mPopupWindow.setHeight((popupListView.getMeasuredHeight() + 10) * arrayList.size());

		mPopupWindow.setOutsideTouchable(true);
		mPopupWindow.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.bg_popupwindow));

		popupListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				mPopupWindow.dismiss();
				switch (position) {
				case 0:
					// 分享
					share();
					break;

				case 1:
					CustomToast.make(StoreItemActivity.this).setText("更多功能升级中...").show();
					break;

				default:
					CustomToast.make(StoreItemActivity.this).setText("更多功能升级中...").show();
					break;
				}

			}
		});
	}

	/**
	 * 分享内容
	 */
	public void share() {
		String shareContent = "Hi,我给大家分享个大家都喜欢的店铺哟!~.~\n就是它了    " + ((Store) mBundle.getSerializable(StoreMode.INTENT_URL)).getName();
		Intent shareIntent = new Intent(Intent.ACTION_SEND);
		shareIntent.putExtra(Intent.EXTRA_TEXT, shareContent);
		shareIntent.setType("text/plain");
		startActivity(Intent.createChooser(shareIntent, "分享到..."));
	}

	/** 初始化PagerSlideTab */
	private void initPagerTab() {
		mPagerTab = (PagerSlideTab) findViewById(R.id.pager_slide_tab_store_item);
		mPagerTab.setShouldExpand(true); // 设置为均等分,须在setViewPager之前设置
		mPagerTab.setViewPager(mViewPager);
	}

	/** 初始化ViewPager */
	private void initViewPager() {
		mFragmentList = new ArrayList<Fragment>();
		mItemMenu = StoreItemMenu.getInstance(mBundle);
		mFragmentList.add(mItemMenu);
		mFragmentList.add(StoreItemInfo.getInstance(mBundle));

		mViewPager = (ViewPager) findViewById(R.id.viewpager_store_item);
		StoreItemAdapter adapter = new StoreItemAdapter(getSupportFragmentManager(), mFragmentList, TITLES);
		mViewPager.setPageMargin(5);
		mViewPager.setAdapter(adapter);
		mViewPager.setVisibility(View.VISIBLE);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.ib_actionbar_back_home:
			finish();
			break;

		case R.id.iv_actionbar_query:

			// TODO 添加查询
			CustomToast.make(mContext).setText("query").show();
			Intent searchIntent = new Intent(StoreItemActivity.this, SearchActivity.class);
			searchIntent.putExtra(Constants.SEARCH_TYPE, Constants.SEARCH_TYPE_GOODS);
			startActivity(searchIntent);
			break;

		case R.id.iv_actionbar_more:
			// TODO 添加更多
			mPopupWindow.showAsDropDown(mMore);

			break;

		default:
			break;
		}
	}

	@Override
	public void setNetWork(Context context) {
		UIHelper.showNetWorkSetup(this);
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		Log.d(TAG, "onActivityResult  requestCode = " + requestCode + "  resultCode = " + resultCode);

		super.onActivityResult(requestCode, requestCode, intent);
		if (mItemMenu != null) {
			mItemMenu.onActivityResult(requestCode, resultCode, intent);
		}

		switch (requestCode) {
		case Constants.SETUP_NETWORK_REQUEST_CODE:
			// 网络状态改变了
			if (resultCode == Constants.SETUP_NETWORK_RESULT_CODE) {
				initShow();
			}
			break;

		default:
			break;
		}
	}
}
